package com.tancw.viewacty;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.tancw.utils.ImgUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class DownloadActivity extends AppCompatActivity {

    private TextView etUser;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_download);

        Intent intent = getIntent();
        String list = intent.getStringExtra("list");
        try {
            JSONArray ays = new JSONArray(list);
            ListView listView = findViewById(R.id.dwn_view);
            MyAdapter myAdapter = new MyAdapter(ays);
            listView.setAdapter(myAdapter);
            Log.d("DWN", "下载的页面加载完成");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }


    class ImgAdapter extends BaseAdapter {

        private ArrayList<Bitmap> uris;

        public ImgAdapter(ArrayList<Bitmap> uris) {
            this.uris = uris;
        }

        @Override
        public int getCount() {
            return uris.size();
        }

        @Override
        public Object getItem(int position) {
            return uris.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(DownloadActivity.this, R.layout.griditem_addpic, null);
            ImageView imageView = view.findViewById(R.id.reg_img);
            imageView.setImageBitmap(uris.get(position));
            imageView.setOnClickListener(v -> {
                Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                bitmapLoader(bitmap);
            });
            return view;
        }
    }

    private void bitmapLoader(Bitmap bitmap) {
        final Dialog dialog = new Dialog(DownloadActivity.this);
        ImageView image = new ImageView(DownloadActivity.this);
        image.setImageBitmap(bitmap);
        dialog.setContentView(image);
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dialog.show();
        image.setOnClickListener(v -> {
            dialog.cancel();
        });
    }


    public class MyAdapter extends BaseAdapter {

        private JSONArray datas;

        public MyAdapter(JSONArray datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.length();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(DownloadActivity.this, R.layout.download_item, null);
            buildItem(view, position);
            return view;
        }

        private void buildItem(View view, int position) {
            try {
                JSONObject data = new JSONObject((String) datas.get(position));
                TextView view_user = view.findViewById(R.id.dwn_item_user);
                view_user.setText(data.getString("user"));

                TextView view_thing = view.findViewById(R.id.dwn_item_thing);
                view_thing.setText(data.getString("thing"));

            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
